//
//  ZGLNewsCell.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/10/25.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "ZGLNewsCell.h"
@interface ZGLNewsCell ()

@property (nonatomic, strong) UIImageView *FigureImage;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *mediaLabel; //来源


@end


@implementation ZGLNewsCell
+ (instancetype)NewsCellWithTableView:(UITableView *)tablwView
{
    static NSString *ID = @"NewsCellId";
    ZGLNewsCell *cell = [tablwView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[ZGLNewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
//        self.backgroundColor = ViewBackgroundColor;
        
        [self setupUI];
        
        [HKUIUtils addLineInView:self.contentView color:BorderColor top:NO leftMargin:15 rightMargin:15];
    }
    
    
    return self;
}

//处理图片
- (void)downImageWithUrl:(NSString *)urlStr ScaleRate:(CGFloat)scaleRate
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSURL *url = [NSURL URLWithString:urlStr];
        //下载图片
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        UIImage *image1 = [UIImage imageWithData:imageData];
        CGFloat w = image1.size.width;
        CGFloat h = image1.size.height;
        
        UIImage *image = [HKUIUtils scaleImageToSize:image1 scaleRate:scaleRate];
        CGFloat W = image.size.width;
        CGFloat H = image.size.height;
        
//        return image;
        dispatch_async(dispatch_get_main_queue(), ^{

//            UIImage *Image = [[UIImage alloc]init];
            [self.FigureImage setImage:image];

        });
    });
}

#pragma mark - getter*setter

- (void)setModel:(ZGLNewsModel *)model
{
    _model = model;
    [self.titleLabel setText:model.title lineSpacing:5.0f];
    
    NSString *urlStr = model.img.u;
    [self downImageWithUrl:urlStr ScaleRate:0.6];
    
//    NSURL *url = [NSURL URLWithString:urlStr];
//    [self.FigureImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"img_loading_default"]];
    
    self.timeLabel.text = [PublicMethod TimeCompareWith:model.intime];
    self.mediaLabel.text = model.media_name;
    
}

- (void)setupUI
{
    [self.contentView addSubview:self.FigureImage];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.mediaLabel];
    
    [_FigureImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.contentView).offset(15);
        make.height.mas_equalTo(75);
        CGFloat w = UI_SCREEN_WIDTH/3;
        if (UI_SCREEN_WIDTH>320.0) {
            w = w - 20;
        }
        make.width.mas_equalTo(w);
        make.bottom.equalTo(self.contentView).offset(-15);
        
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(_FigureImage.mas_left).offset(-10);
        make.top.equalTo(_FigureImage);
        
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_titleLabel);
        make.bottom.equalTo(_FigureImage);
        
    }];
    
    [_mediaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_timeLabel.mas_right).offset(5);
        make.bottom.equalTo(_timeLabel);
        
    }];
    
}

- (UIImageView *)FigureImage
{
    if (!_FigureImage) {
        _FigureImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_loading_default"]];
    }
    return _FigureImage;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        
        //Arial-BoldMT,Helvetica-Bold,ArialMT,Arial-ItalicMT,Arial-BoldItalicMT
        [_titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:FontSize(17)]];
//        [_titleLabel setFont:[UIFont systemFontOfSize:FontSize(17)]];
        _titleLabel.numberOfLines = 0;
        _titleLabel.text = @"皇马将帅失和?";
    }
    return _titleLabel;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = kFONT_SIZE(13);
        _timeLabel.textColor = detailTextColor;
        
        _timeLabel.text = @"3小时前";
    }
    return _timeLabel;
}

-(UILabel *)mediaLabel
{
    if (!_mediaLabel) {
        _mediaLabel = [[UILabel alloc]init];
        _mediaLabel.font = kFONT_SIZE(13);
        _mediaLabel.textColor = detailTextColor;
        
        _mediaLabel.text = @"体育";
    }
    return _mediaLabel;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
